package com.swinginwind.flexutils.functions
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.*;
	
	/**
	 * @author jimmy
	 * @E-mail: swinginwind@gmail.com
	 * @version 1.0.0
	 * 创建时间：2013-8-26 上午11:11:37
	 * 
	 */
	public class UtilsComponent
	{
		public function UtilsComponent()
		{
		}
		
		
		/**
		 * 计算弹出层的位置, 确保不会超出父元素边界
		 */ 
		public static function calculatePopupPosition(child:DisplayObject, container:DisplayObject, mousePoint:Point):void
		{
			if(!child || !container || !mousePoint)
				return;
			var point:Point = container.globalToLocal(mousePoint);
			child.x = point.x + 50;
			child.y = point.y;
			
			if(child.x > (container.width - child.width))
				child.x = point.x - child.width;
			if(child.y + child.height > container.height)
				child.y = container.height - child.height;
			if(child.y < 0)
				child.y = 0;
		}
		
		
		public static function bindEvent(target:Object, eventType:String, func:Function):void
		{
			switch(eventType)
			{
				case "doubleClick":
					target.addEventListener("doubleClick", func);
					var count:int = 0;
					target.addEventListener("click", function(event:MouseEvent):void {
						count ++;
						if(count == 2)
						{
							clearTimeout(num);
							count = 0;
							func(event);
							return;
						}
						var num:Number = setTimeout(function():void {
							clearTimeout(num);
							count = 0;
						}, 200);
					}
					);
					break;
			}
		}	
		
		
	}
}